<?php
/**
 * php GEM Project
 * 
 * LICENSE
 * 
 * PHP System Architecture Enabling Rapid Application Deployment and Scalability
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * @copyright Copyright (C) 2010 Zeletron LLC (http://zeletron.com)
 * @license http://gem.zeletron.com/license
 */

/*
 * GEM/Model/Mapper/FileMapper.php
 * 
 * 
 * @author David Joly
 * @link http://zeletron.com
 * 
 * @category GEM
 * @package GEM_Model
 * @subpackage GEM_Model_Mapper 
 */
class GEM_Model_Mapper_FileMapper extends GEM_Model_Mapper_Mapper
{	
	/**
	 * Prepared Insert Statement
	 * 
	 * @var PDOStatement
	 */
	protected $_insertStm;
	
	/**
	 * Constructor
	 * 
	 * @return void
	 */
	public function __construct()
	{
		parent::__construct();
		
		//Initialize Prepared Statements
		$this->_insertStm = $this->_db->prepare($this->_insertStm());
	}
	
	/**
	 * Override by extending file classes. File collection class
	 * collects any files in the database regardless of type.
	 * For extending class to limit collections to related files,
	 * you must specify the 'type' field as well. You will
	 * also need to override the selectAllStm() to include
	 * a "WHERE type = 'type'" clause. For a concrete implementation,
	 * take a look at GEM_Model_Mapper_ImageMapper.
	 * @see GEM_Model_Mapper#_selectAllStm()
	 * @see GEM_Model_ImageMapper#_insertStm()
	 * @see GEM_Model_ImageMapper#_selectAllStm()
	 * 
	 * @return string
	 */
	protected function _insertStm()
	{
		return "INSERT INTO file SET
				description = :description,
				caption = :caption,
				path = :path,
				createdDate = :createdDate,
				owner = :owner";
	}
	
	/**
	 * Used by parent class to dynamically build common prepared
	 * statements.
	 * @see GEM_Model_Mapper_Mapper#_getTable()
	 * 
	 * @return string
	 */
	protected function _getTable()
	{
		return 'file';
	}
	
	/**
	 * Used by parent class to dynamically locate collection.
	 * @see GEM_Model_Mapper_Mapper#getCollection()
	 * 
	 * @return string
	 */
	protected function _getClassType()
	{
		return 'File';
	}
	
	/**
	 * @param array $fields
	 * @return GEM_Model_Domain_File
	 */
	protected function _doCreateObject(array $fields)
	{
		$file = new GEM_Model_Domain_File($fields['id'],$fields['path']);
		$file->setDescription($fields['description'])
			 ->setCaption($fields['caption'])
			 ->setCreatedDate($fields['createdDate'])
			 ->setModifiedDate($fields['modifiedDate'])
			 ->setOwner($fields['owner']);
		return $file;
	}
	
	/**
	 * Inserts a new file into the database.
	 * 
	 * @param GEM_Model_Domain_File $obj
	 * @return void
	 */
	protected function _doInsert(GEM_Model_Domain_Object $obj)
	{
		if(!($obj instanceof GEM_Model_Domain_File))
			throw new GEM_Model_Domain_Exception("Object type GEM_Model_Domain_File Expected.");
		$this->_insertStm->execute(array(
			':description'	=> $obj->getDescription(),
			':caption'		=> $obj->getCaption(),
			':path'			=> $obj->getPath(),
			':createdDate'	=> date('Y-m-d H:i:s'),
			':owner'		=> $obj->getOwner()->getId()
		));
		$obj->setId($this->_db->lastInsertId());
	}
	
	/**
	 * @param GEM_Model_Domain_File $obj
	 * @return void
	 */
	protected function _doDelete(GEM_Model_Domain_Object $obj)
	{
		if(!($obj instanceof GEM_Model_Domain_File))
			throw new GEM_Model_Domain_Exception("Object type GEM_Model_Domain_File Expected.");
		$this->_deleteStm->execute(array($obj->getId));
	} 
	
	/**
	 * Returns all files with the specified owner.
	 * 
	 * @param mixed int | GEM_Model_Domain_User $owner Either a user id or a user object.
	 * @return GEM_Model_Mapper_Collection
	 */
	public function findByOwner($owner)
	{
		if(is_numeric($owner)){
			$owner = (int)$owner;
		} elseif($owner instanceof GEM_Model_Domain_User){
			$owner = $owner->getId();
		} else {
			throw new GEM_Model_Mapper_Exception(__METHOD__ . " expects either a numeric user id or a GEM_Model_Domain_User object.");
		}
		$stm = $this->_db->prepare("SELECT * FROM {$this->_getTable()} WHERE owner = :owner");
		return $this->getCollection($stm,array(':owner' => $owner));
	}
	
}